package cms.util;

import cms.entity.Bill;
import cms.service.csvService;
import com.csvreader.CsvReader;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;

import static cms.util.arrToObject.fillZero;
import static cms.util.arrToObject.invokeMethodBill;

public class csvResolve {

    /*public static void main(String[] args) {
        try {
            ArrayList<String[]> csv=new ArrayList<String[]>();

            String csvPath="C:\\Users\\dinstar\\Desktop\\bill2.csv";

            CsvReader csvReader=new CsvReader(csvPath,',', Charset.forName("UTF-8"));
            //csvReader.readHeaders();

            while (csvReader.readRecord()){
                // System.out.println(csvReader.getRawRecord()+"||||||");
                csv.add(csvReader.getValues());
            }
            csvReader.close();

            for(int i=0;i<csv.size();i++){
                String[] cell=csv.get(0);
                for (String temp:cell
                        ) {
                    System.out.print("--->"+temp);
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }*/

    @Autowired
    private csvService csvService1;

    public void test() {
        try {
            ArrayList<String[]> bill=new ArrayList<String[]>();

            String billPath="C:\\Users\\dinstar\\Desktop\\bill2.csv";

            CsvReader csvReader=new CsvReader(billPath,',', Charset.forName("UTF-8"));

            while (csvReader.readRecord()){
                bill.add(csvReader.getValues());
            }
            csvReader.close();


            // for (int i=1;i<bill.size();i++){

            String[] tempArr=bill.get(3);

            tempArr=fillZero(tempArr);

            Bill temp=new Bill();

            temp=(Bill)invokeMethodBill(tempArr);

            temp.setUserId(62169);
            temp.setBillId(2);
            System.out.println("---");
            csvService1.insert(temp);
            System.out.println("--->");


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
